const nodemailer = require("nodemailer");
module.exports = (app) => {
  const {
    config: { smtp },
  } = app;
  const $nodemailer = nodemailer.createTransport(smtp, {
    from: smtp.from || smtp.auth.user,
  });
  app.$emailer = {
    nodemailer: $nodemailer,
    sendMail: (message, options = {}) =>
      new Promise((resolve, reject) => {
        $nodemailer.sendMail(message, (error, info) => {
          if (error) {
            console.log("Error occurred");
            console.log(error.message);
            reject(options.errorText || error.message);
          } else {
            console.log("Message sent successfully!");
            console.log(nodemailer.getTestMessageUrl(info));
            resolve(options.successText || "发送成功");
          }
          transporter.close();
        });
      }),
  };
};
